using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Xml;

using Mirkuz.Mapping;

namespace KMLCreator
{

  /// <summary>
  /// http://earth.google.com/kml/kml_tags_21.html
  /// </summary>

  enum KMLAltitudeMode : byte
  {
    Absolute,
    ClampToGround,
    RelativeToGround
  }

  class KMLPoint
  {
    MapPoint _Coords;
    KMLAltitudeMode _AltitudeMode = KMLAltitudeMode.ClampToGround;

    String _KMLFilePath = "\\";
    String _KMLFileName = "Point.kml";

    String _Name = "";
    String _Description = "";
      
    public KMLPoint()
    { }

    public KMLPoint(MapPoint Coords)
    {
      _Coords = Coords;
    }

    public KMLPoint(MapPoint Coords, String Name)
    {
      _Name = Name;
      _Coords = Coords;
    }

    public KMLPoint(MapPoint Coords, String Name, String Description)
    {
      _Description = Description;
      _Name = Name;
      _Coords = Coords;
    }

    public MapPoint Coords
    {
      get { return _Coords; }
      set { _Coords = value; }
    }

    public KMLAltitudeMode AltitudeMode
    {
      get { return _AltitudeMode; }
      set { _AltitudeMode = value; }
    }

    public string KMLFilePath
    {
      get { return _KMLFilePath; }
      set 
      { 
        _KMLFilePath = value;

        if (_KMLFilePath.Substring(_KMLFilePath.Length - 1, 1) != "\\")
          _KMLFilePath += "\\";

      }
    }

    public string KMLFileName
    {
      get { return _KMLFileName; }
      set { _KMLFileName = value; }
    }

    public string Name
    {
      get { return _Name; }
      set { _Name = value; }
    }

    public string Description
    {
      get { return _Description; }
      set { _Description = value; }
    }

    public void Save()
    {
      string FullFilePath = _KMLFilePath + _KMLFileName;

      XmlTextWriter xtr = new XmlTextWriter(FullFilePath, null);

      xtr.WriteStartDocument();

      xtr.WriteStartElement("kml");
      xtr.WriteString(" xmlns=\"http://earth.google.com/kml/2.1\"");
      {
        xtr.WriteStartElement("Placemark");
        {
          xtr.WriteStartElement("Name");
          xtr.WriteString(_Name);
          xtr.WriteEndElement();

          xtr.WriteStartElement("Description");
          xtr.WriteString(_Description);
          xtr.WriteEndElement();
     
          xtr.WriteStartElement("Point");
          {
            xtr.WriteStartElement("coordinates");
            xtr.WriteString(
                _Coords.Longitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + ","
                + _Coords.Latitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + ","
                + _Coords.Altitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat));
            xtr.WriteEndElement();
          }
          xtr.WriteEndElement();
        }
        xtr.WriteEndElement();
      }
      xtr.WriteEndElement();

      xtr.WriteEndDocument();

      xtr.Close();

    }

  }

  class KMLLine
  {
      List<MapPoint> _Coords = new List<MapPoint>();
    KMLAltitudeMode _AltitudeMode = KMLAltitudeMode.ClampToGround;

    String _KMLFilePath = "\\";
    String _KMLFileName = "Line.kml";

    String _Name = "";
    String _Description = "";


    public KMLLine(Mirkuz.Mapping.MapTrack Track, String Name, String Description)
    {
        _Description = Description;
        _Name = Name;
        _Coords = Track.TrackPoints;
    }

    public List<MapPoint> Coords
    {
      get { return _Coords; }
      set { _Coords = value; }
    }

    public KMLAltitudeMode AltitudeMode
    {
      get { return _AltitudeMode; }
      set { _AltitudeMode = value; }
    }

    public string KMLFilePath
    {
      get { return _KMLFilePath; }
      set 
      { 
        _KMLFilePath = value;

        if (_KMLFilePath.Substring(_KMLFilePath.Length - 1, 1) != "\\")
          _KMLFilePath += "\\";

      }
    }

    public string KMLFileName
    {
      get { return _KMLFileName; }
      set { _KMLFileName = value; }
    }

    public string Name
    {
      get { return _Name; }
      set { _Name = value; }
    }

    public string Description
    {
      get { return _Description; }
      set { _Description = value; }
    }

    public void Save()
    {
      string FullFilePath = _KMLFilePath + _KMLFileName;

      try
      {
          if (File.Exists(FullFilePath))
              File.Delete(FullFilePath);
      }
      catch { }

      XmlTextWriter xtr = new XmlTextWriter(FullFilePath,null);

      xtr.WriteStartDocument();

      xtr.WriteStartElement("kml");
      xtr.WriteString(" xmlns=\"http://earth.google.com/kml/2.1\"");

      {
        xtr.WriteStartElement("Placemark");
        {
          xtr.WriteStartElement("Name");
          xtr.WriteString(_Name);
          xtr.WriteEndElement();

          xtr.WriteStartElement("Description");
          xtr.WriteString(_Description);
          xtr.WriteEndElement();          

          //Style region
          xtr.WriteStartElement("Style");
          {
            xtr.WriteStartElement("LineStyle");
            {
              xtr.WriteStartElement("color");
              xtr.WriteString("ffff00ff");
              xtr.WriteEndElement();

              xtr.WriteStartElement("width");
              xtr.WriteString("4");
              xtr.WriteEndElement();
            }
            xtr.WriteEndElement();
          }
          xtr.WriteEndElement();

          //Line Coords
          xtr.WriteStartElement("LineString");
          {
            xtr.WriteStartElement("coordinates");
            xtr.WriteString(GetCoordinateString());
            xtr.WriteEndElement();
          }
          xtr.WriteEndElement();
        }
        xtr.WriteEndElement();
      }
      xtr.WriteEndElement();

      xtr.WriteEndDocument();

      xtr.Close();

    }

    private String GetCoordinateString()
    {
        StringBuilder sb = new StringBuilder();

        for (int x = 0; x < _Coords.Count; x++)
        {
            sb.Append(
              _Coords[x].Longitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + "," +
              _Coords[x].Latitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + "," +
              _Coords[x].Altitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + " ");
        }

        return sb.ToString();

    }
  }
}
